/**
 * 
 */
Ext.ns("positionApply");
positionApply.init = function() {
	positionApply.count;// 可安排的考生数目
	positionApply.countOk;// 确定的考生数目
	positionApply.rmenuClick;// 右键点中的单行数据
	positionApply.conductionId;// 现点击考点ID
	positionApply.conductionIdr;// 原始考点ID
	positionApply.personCountLimit;// 限定考生最低人数
	positionApply.lcm = new Ext.grid.ColumnModel([new Ext.grid.RowNumberer(), {
				header : '考场名称',
				dataIndex : 'name',
				sortable : true,
				width : 160
			}, {
				header : '容纳人数',
				dataIndex : 'count',
				sortable : true,
				width : 80
			}]);
	positionApply.rcm = new Ext.grid.ColumnModel([new Ext.grid.RowNumberer(), {
				header : '考场名称',
				dataIndex : 'name',
				sortable : true,
				width : 160
			}, {
				header : '容纳人数',
				dataIndex : 'count',
				sortable : true,
				width : 80
			}]);
	positionApply.lstore = new Ext.data.JsonStore({
				url : 'center/showPositionListForCenter',
				totalProperty : 'total',
				root : 'positions',
				fields : [{
							name : "id",
							mapping : 'examPosition_Id',
							type : 'int'
						}, {
							name : "name",
							mapping : 'examPosition_Name',
							type : 'string'
						}, {
							name : "count",
							mapping : 'examPosition_PersonCount',
							type : 'int'
						}]
			});
	positionApply.rstore = new Ext.data.JsonStore({
				url : 'store/showGoodValue.action',
				totalProperty : 'total',
				root : 'goodValues',
				fields : [{
							name : "id",
							mapping : 'examPosition_Id',
							type : 'int'
						}, {
							name : "name",
							mapping : 'examPosition_Name',
							type : 'string'
						}, {
							name : "count",
							mapping : 'examPosition_PersonCount',
							type : 'int'
						}]
			});

	positionApply.leftPanel = new Ext.grid.GridPanel({
				cm : positionApply.lcm,
				store : positionApply.lstore,
				enableDragDrop : true,
				ddGroup : 'gridDDGroup',
				scrollOffset : 0,
				stripeRows : true,
				viewConfig : {
					columnsText : '选择显示列',
					forceFit : true
				},
				loadMask : true,
				height : 500,
				frame : true,
				columnWidth : .5,
				tbar : new Ext.Toolbar(["可分配人数：", {
							id : 'positionApplyPersonCount',
							xtype : 'displayfield',
							value : 0
						}, '-', "最低人数：", {
							id : 'positionApplyPersonCountLimit',
							xtype : 'displayfield',
							value : 0
						}, '-', {
							text : "清楚选择",
							handler : function() {
								positionApply.rstore.removeAll();
								Ext.getCmp("positionApplyIncludeCount").setValue(0);
								Ext.getCmp("positionApplyCountOk").setValue(0);
							}
						}])
			});
	positionApply.rightPanel = new Ext.grid.GridPanel({
		cm : positionApply.rcm,
		store : positionApply.rstore,
		enableDragDrop : false,
		ddGroup : 'gridDDGroup',
		stripeRows : true,
		columnWidth : .5,
		height : 500,
		frame : true,
		viewConfig : {
			forceFit : true
		},
		tbar : new Ext.Toolbar(["可容人数：", {
					id : 'positionApplyIncludeCount',
					xtype : 'displayfield',
					value : 0
				}, '-', '考生数量：', {
					id : 'positionApplyCountOk',
					xtype : 'numberfield',
					allowDecimals : false,
					allowNegative : false,
					allowBlank : false,
					width : 30,
					listeners : {
						'blur' : function() {
							positionApply.countOk = Ext
									.getCmp("positionApplyCountOk").getValue();
						}
					}
				}, '-', {
					id : 'positionApplyConfirmButton',
					text : '确定考场',
					handler : function() {
						if (positionApply.countOk == 0
								|| positionApply.countOk == null) {
							Ext.Msg.show({
										title : '错误提示',
										msg : '对不起，考生数量不能为空',
										buttons : Ext.MessageBox.OK,
										icon : Ext.MessageBox.WARNING
									});
						} else if (positionApply.countOk > positionApply.count) {
							Ext.Msg.show({
										title : '错误提示',
										msg : '对不起，考生数量不能大于可分配考生数量！',
										buttons : Ext.MessageBox.OK,
										icon : Ext.MessageBox.WARNING
									});

						} else if (positionApply.personCountLimit > positionApply.countOk) {
							Ext.Msg.show({
										title : '错误提示',
										msg : '对不起，考生数量不能小于最小限定值！',
										buttons : Ext.MessageBox.OK,
										icon : Ext.MessageBox.WARNING
									});
						} else {
							var position_Ids = []; // 要删除的id
							Ext.each(positionApply.rstore.getRange(), function(
											item) {
										position_Ids.push(item.data.id);
									});
							if (position_Ids.length == 0) {
								Ext.Msg.show({
											title : '错误提示',
											msg : '对不起，请从左到右拖动考场进行分配！',
											buttons : Ext.MessageBox.OK,
											icon : Ext.MessageBox.WARNING
										});
							} else {
								Ext.Ajax.request({
									url : 'center/arrangeForPosition.action',
									method : 'post',
									params : {
										personCount : positionApply.countOk,
										conduction_Id : positionApply.conductionId,
										positionIds : position_Ids
									},
									success : function(response) {
										var obj = Ext
												.decode(response.responseText);
										if (obj.success == false) {
											Ext.Msg.alert("提示", obj.msg);
										} else {
											Ext.Msg.alert("提示", obj.msg);
											conductionApply.sumPersonCount -= positionApply.countOk;
											positionApply.Main.hide();
										}
									},
									failure : function(response) {
									}
								});
							}
						}
					}
				}])
	});
	positionApply.rmenu = new Ext.menu.Menu({
				items : [{
					id : 'positionMenu',
					text : '删除该选项',
					handler : function() {
						Ext.getCmp("positionApplyIncludeCount").setValue(Ext
								.getCmp("positionApplyIncludeCount").getValue()
								- positionApply.rmenuClick.get("count"));
						positionApply.rstore.remove(positionApply.rmenuClick);
					}
				}]
			});
	positionApply.Main = new Ext.Window({
				region : "center",
				id : "positionApplyMainPanel",
				deferredRender : true,
				closeAction : 'hide',
				loadMask : true,
				width : 600,
				height : 400,
				closable : true,
				modal : true,
				progress : true,
				draggable : true,
				resizable : false,
				layout : 'column',
				autoScroll:true,
				viewConfig : {
					forceFit : true
				},
				items : [positionApply.leftPanel, positionApply.rightPanel]
			});

	positionApply.Main.on("show", function() {
		positionApply.Main.setTitle(conductionApply.menuClick.get("name")
				+ '考场安排窗口');
		Ext.getCmp("positionApplyPersonCount").setValue(positionApply.count);
		new Ext.dd.DropTarget(positionApply.rightPanel.getView().scroller.dom,
				{
					ddGroup : 'gridDDGroup',
					copy : false,
					notifyDrop : function(ddSource, e, data) {
						var records = ddSource.dragData.selections;
						Ext.each(records, function(item, index, allitems) {
							if (positionApply.rstore.find("id", item.data.id) == -1) {
								positionApply.rightPanel.store.add(item);
								Ext
										.getCmp("positionApplyIncludeCount")
										.setValue(parseInt(Ext
												.getCmp("positionApplyIncludeCount")
												.getValue())
												+ parseInt(item.data.count));
							} else {
								Ext.Msg.show({
											title : '错误提示',
											msg : '对不起，您已经添加了‘'
													+ item.data.name + "'考场",
											buttons : Ext.MessageBox.OK,
											icon : Ext.MessageBox.WARNING
										});
							}
						});
					}
				});
		Ext.getCmp("positionApplyPersonCount")
				.setValue(conductionApply.sumPersonCount);
		Ext.getCmp("positionApplyPersonCountLimit")
				.setValue(positionApply.personCountLimit);
	});
	positionApply.rightPanel.on("rowcontextmenu", function(grid, rowIndex, e) {
				e.preventDefault();
				positionApply.rightPanel.getSelectionModel()
						.selectRow(rowIndex);
				positionApply.rmenu.showAt(e.getXY());
				positionApply.rmenuClick = positionApply.rightPanel
						.getSelectionModel().getSelected(rowIndex);
			});
	positionApply.Main.on("beforeshow", function() {
				if (positionApply.conductionIdr != positionApply.conductionId) {
					positionApply.rstore.removeAll(false);
					Ext.getCmp("positionApplyIncludeCount").setValue(0);
					Ext.getCmp("positionApplyCountOk").setValue(0);
				}
			});
	positionApply.Main.on("beforehide", function() {
				positionApply.conductionIdr = positionApply.conductionId;
			});
};
Ext.onReady(positionApply.init);